/***************************************************************************
 *   Copyright (C) 2009 by p1r0                                            *
 *   lucifers.preacher@gmail.com                                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#define MAX_BUFFER 2048

#ifdef __DEBUG_V8__
    #include <v8-debug.h>
#else
    #include <v8.h>
#endif

#include <iostream>
#include "files.h"
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <unistd.h>

#include "corefunctions.h"
#include "auxiliary.h"
#include "screen.h"

using namespace std;
using namespace v8;

int main(int argc, char* argv[]) 
{
    char* memblock;
    string jsSource = "";


    //Check for input parameters

    if(argc < 2)
    {
        cout << "No input file" << endl;
        return 1;
    }

    //try to load the file, if not exit
    if(!Files::load(argv[1], memblock))
    {
        cout << "Could not load script file" << endl;
        return 1;
    }
    else
    {
        jsSource = string(memblock);
        delete[] memblock;

        //remove the shell interpreter line
        if(jsSource[0] == '#')
        {
            //there is a intereter line, remove it before passing the
            //script to V8
            size_t crlfPos = jsSource.find("\n");
            if (crlfPos != string::npos)
            {
                jsSource.erase(0, crlfPos);
            }
        }
    }


    // Create a stack-allocated handle scope.
    HandleScope handle_scope;


    Handle<ObjectTemplate> global_templ = ObjectTemplate::New();
    global_templ->Set(String::New("include"), FunctionTemplate::New(IncludeCallback));
    global_templ->Set(String::New("print"), FunctionTemplate::New(PrintCallback));
    global_templ->Set(String::New("getLine"), FunctionTemplate::New(GetLineCallback));
    global_templ->Set(String::New("clear"), FunctionTemplate::New(ClearCallback));
    global_templ->Set(String::New("call"), FunctionTemplate::New(CallCallback));
    global_templ->Set(String::New("sleep"), FunctionTemplate::New(SleepCallback));
    global_templ->Set(String::New("getchar"), FunctionTemplate::New(GetcharCallback));
    //ncurses functions
    global_templ->Set(String::New("nc_initscr"), FunctionTemplate::New(InitscrCallback));
    global_templ->Set(String::New("nc_refresh"), FunctionTemplate::New(RefreshCallback));
    global_templ->Set(String::New("nc_endwin"), FunctionTemplate::New(EndwinCallback));
    global_templ->Set(String::New("nc_move"), FunctionTemplate::New(MoveCallback));
    global_templ->Set(String::New("nc_printw"), FunctionTemplate::New(PrintwCallback));


    // Create a new context.
    Persistent<Context> context = Context::New(NULL, global_templ);
    
    // Enter the created context for compiling and
    // running the script. 
    Context::Scope context_scope(context);

    exposeArgs(argc, argv);

    // Create a string containing the JavaScript source code.
    Handle<String> source = String::New(jsSource.c_str());

    // Compile the source code.
    Handle<Script> script = Script::Compile(source);
    
    // Run the script to get the result.
    Handle<Value> result = script->Run();

    // Dispose the persistent context.
    context.Dispose();

    return 0;
}
